<template>
  <div id="liveLine">
    <div ref="chart01" class="chart01"></div>
  </div>
</template>

<script>
export default {
  name: 'liveLine',
  components: {},
  props: {
    xData: Array | Object,
    series1: Array | Object,
    series2: Array | Object,
    name1: String,
    name2: String,
    title: String
  },
  data() {
    return {};
  },
  computed: {},
  watch: {
    xData: {
      handler(n, o) {
        this.drawLiveLine();
      },
      deep: true
    }
  },

  created() {},
  mounted() {
    this.drawLiveLine();
  },

  methods: {
    drawLiveLine() {
      let that = this;
      let mychart = that.$echarts.init(that.$refs.chart01);
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'line', // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: '#e5e5e5'
            }
          },
          extraCssText: 'z-index: 9', //设置消息提示框的层级
          formatter: function (params) {
            let result01 = '';
            params.forEach(function (item) {
              result01 +=
                item.marker + ' ' + item.seriesName + ' : ' + that.format_num(item.value) + '</br>';
            });
            return params[0].name + '</br>' + result01;
          }
        },
        legend: {
          //   data: [
          //     "商品总数",
          //     "动销商品数",
          //     "直播销量",
          //     "直播销售额",
          //   ],
          icon: 'rect ',
          top: 16,
          itemWidth: 20,
          itemHeight: 3,
          itemGap: 30,
          textStyle: {
            color: '#555'
          }
        },
        grid: {
          left: '55',
          right: '55',
          bottom: '30',
          containLabel: false
        },
        // dataZoom: [  //显示下面区域选择
        //   {
        //     type: "slider",
        //     realtime: true,
        //     start: 0,
        //     end: 7,
        //     xAxisIndex: [0],
        //   },
        // ],
        xAxis: [
          {
            type: 'category',
            data: this.xData,
            boundaryGap: false, //X轴起始位置靠着Y轴
            //设置网格线颜色
            splitLine: {
              show: false,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            // x轴样式
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            },
            axisTick: {
              show: false
            },
            // x轴字体样式
            axisLabel: {
              // interval: 0,
              // rotate: 40,
              margin: 14,
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: this.title ? this.title : this.name1,
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, 20]
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                return that.format_num(value);
              }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          },
          {
            type: 'value',
            name: this.name2,
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, -20]
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                return that.format_num(value);
              }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          }
        ],
        series: [
          {
            name: this.name1,
            type: 'line',
            smooth: true, //true 为平滑曲线，false为直线
            symbol: 'none', //去掉折线上的小圆点
            data: this.series1,
            yAxisIndex: 0,
            itemStyle: {
              normal: {
                areaStyle: {
                  type: 'default',
                  //渐变色实现===
                  color: new this.$echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    //三种由深及浅的颜色
                    [
                      {
                        offset: 0,
                        color: '#FED1B4'
                      },
                      {
                        offset: 1,
                        color: '#FAFAFB'
                      }
                    ]
                  )
                },
                // color: "#2781FF",
                //柱子颜色
                color: '#fd7f2c'
              }
            }
          },
          {
            name: this.name2,
            type: 'line',
            smooth: true, //true 为平滑曲线，false为直线
            symbol: 'none', //去掉折线上的小圆点
            yAxisIndex: 1,
            data: this.series2,
            itemStyle: {
              normal: {
                areaStyle: {
                  type: 'default',
                  //渐变色实现===
                  color: new this.$echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    //三种由深及浅的颜色
                    [
                      {
                        offset: 0,
                        color: '#ACCFFF'
                      },
                      {
                        offset: 1,
                        color: '#FAFAFB'
                      }
                    ]
                  )
                },
                // color: "#fd7f2c",
                //柱子颜色
                color: '#2781FF'
              }
            }
          }
        ]
      };
      mychart.setOption(option);
      //   窗口自适应
      setTimeout(() => {
        window.addEventListener('resize', function () {
          mychart.resize();
        });
      }, 200);
    }
  }
};
</script>
<style lang='less' scoped>
#liveLine {
  width: 100%;
  height: 100%;
  // margin-top: 36px;
  .chart01 {
    width: 100%;
    height: 100%;
    overflow: hidden;
    clear: both;
  }
}
</style>